<?php
Yii::import('application.vendors.Browser');
class DefaultController extends FrontController
{
    public $pageId;
    
    public function init(){
        $browser = new Browser();
        if($browser->isMobile() || in_array($browser->getPlatform(), array(Browser::PLATFORM_IPAD, Browser::PLATFORM_IPHONE, Browser::PLATFORM_ANDROID))) {
            if (defined('Settings::TABLET_SITE') && defined('Settings::TABLET_REDIRECTED_WORKFLOW') && Yii::app()->cmsManager->site['name'] == 'serta.com')
            {
                $url = FSM::_run('Cms.workflow.findWorkflowBySNAndWN', array(
                    'site_name'=>Settings::TABLET_SITE,
                    'workflow_name'=>Settings::TABLET_REDIRECTED_WORKFLOW,
                ))->url;
                if (empty($url) === false)
                {
                    
                    if (strpos($url, Yii::app()->request->getHostInfo()) === false)
                    {
                        $this->redirect($url);
                    }
                }
            }
        }
        // for the flash cookie which is set by JS, we must get the cookie 
        // before Yii's cookie validation is turned on
        if (!isset(Yii::app()->request->Cookies['bflash']) || Yii::app()->request->Cookies['bflash']->value == false) 
            Yii::app()->user->setState('HAS_FLASH',0);
        else 
            Yii::app()->user->setState('HAS_FLASH',1);

        Yii::app()->sourceLanguage = 'en_us';
        Yii::app()->language = Yii::app()->cmsManager->site['locale_id'];
        
        parent::init();
        
        // turn security on
        Yii::app()->request->enableCsrfValidation = true;
        Yii::app()->request->enableCookieValidation = true;        
    }
    
    public function getGuestAllowedActions(){
        return array();
    }
        
    public function actionIndex()
    {
        if (Yii::app()->user->isGuest)
        {
            Yii::app()->urlManager->setUrlFormat(CUrlManager::GET_FORMAT);
            $this->redirect(array('/Admin/default/login'));
        }
        Yii::app()->theme = Yii::app()->cmsManager->CmsTheme;
        
        if (Yii::app()->request->getRequestUri() == '/')
        {
            if ($this->isBrowserSupported() == false){
                // not supported user agent, send the to homepage
                $this->redirect(baseUrl().'/index.html');
            }
            $this->render('index');
            return;
        }
        
        $reuqestedPageUrl = Yii::app()->urlManager->RequestedPageUrl;
        $html = FSM::run('Cms.Page.renderPage', array('url' => $reuqestedPageUrl))->html;
        
        //fix Facebook Metadata
        if (Yii::app()->user->hasFlash('fb-meta'))
        {
            Yii::app()->user->getFlash('fb-meta');
            $html = str_replace('<html xmlns="http://www.w3.org/1999/xhtml">', '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">', $html);
        }
        // strip <root> tag
//        $html = substr($html,6,strlen($html)-14);
        $html = str_replace(array('<!DOCTYPE html>'), '', $html);
        // add reference for http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
//        $this->layout = false;
        echo $html;
    }
    
    protected function isBrowserSupported(){
        $agents = array(
            array('Mozila','Gecko/','Firefox/'),                // Firefox
            '(KHTML, like Gecko) Chrome',                       // Google Chrome
            'Opera',                                            // Opera
            'MSIE 10.0','MSIE 9.0','MSIE 8.0','MSIE 7.0',       // MS IE 7+
            array('AppleWebKit','(KHTML, like Gecko)'),         // Safari
            array('Konqueror','(like Gecko)'),                  // Konqueror
        );
        
        $ua = Yii::app()->getRequest()->getUserAgent();
        foreach($agents as $keys)
        {
            if (is_string($keys))
            {
                if (strpos($ua, $keys) !== false) return true;
            }
            else
            {   
                $supported = true;
                foreach($keys as $key)
                    if (strpos($ua, $key) !== false) $supported = false;

                if ($supported) return true;
            }
        }
        
        return false;
    }
}